﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using JumbotCms.Common;

namespace JumbotCms.WebFile.MTag
{
    public partial class _view_threadview : JumbotCms.API.UserCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User_Load("", "html");
            string threadid = Str2Str(q("threadid"));
            string mtagid = Str2Str(q("mtagid"));
            if (q("act") != "save")
            {
                string PageStr = GetPageHtml("mtag_threadview");
                bool _isclose = false;
                doh.Reset();
                doh.ConditionExpress = "id=" + mtagid;
                if (doh.GetValue("jcms_normal_mtag", "ViewPower").ToString() == "1")//表示封闭
                    _isclose = true;
                else
                    _isclose = false;
                doh.Reset();
                doh.SqlCmd = "SELECT a.id as mtagid,a.Title as mtagname,a.Img as mtagImg,a.MemberTotal as mtagMemberTotal,a.Proclaim as mtagProclaim,b.id as mtagsortid,b.Title as mtagsortname FROM " +
                    " [jcms_normal_mtag] as a left join [jcms_normal_mtag_sort] as b" +
                    " on b.id=a.SortId WHERE a.id=" + mtagid;
                if (_isclose)
                    doh.SqlCmd += " and A.Id in (select MTagId from [jcms_normal_mtag_member] where Power>-2 and UserId=" + UserId + ")";
                DataTable dtMTag = doh.GetDataTable();
                if (dtMTag.Rows.Count > 0)
                {
                    for (int i = 0; i < dtMTag.Columns.Count; i++)
                    {
                        PageStr = PageStr.Replace("{$_" + dtMTag.Columns[i].ColumnName.ToLower() + "$}", dtMTag.Rows[0][i].ToString());
                    }
                    dtMTag.Clear();
                    doh.Reset();
                    doh.SqlCmd = "SELECT a.id as threadid,a.Title as threadtitle,a.Content as threadContent,a.IsGood as threadisgood,a.IsHead as threadishead,a.AddTime as threadAddTime,b.id as threaduserid,b.UserName as threadusername FROM " +
                        " [jcms_normal_mtag_thread] as a left join [jcms_normal_user] as b" +
                        " on b.id=a.UserId " +
                        " WHERE a.id=" + threadid;
                    DataTable dtThread = doh.GetDataTable();
                    if (dtThread.Rows.Count > 0)
                    {
                        for (int j = 0; j < dtThread.Columns.Count; j++)
                        {
                            PageStr = PageStr.Replace("{$_" + dtThread.Columns[j].ColumnName.ToLower() + "$}", dtThread.Rows[0][j].ToString());
                        }
                        dtThread.Clear();
                        Response.Write(PageStr);
                    }
                    else
                    {
                        Response.Write("参数错误");
                        dtThread.Clear();
                    }
                }
                else
                {
                    Response.Write("权限不足");
                    dtMTag.Clear();
                }
            }
            else
            {
                if (!CheckFormUrl())
                    Response.End();
                string _code = f("txtCode");
                if (Jumbot.Common.Cookie.GetValue("ValidateCode") != _code)
                {
                    Response.Write("JumbotCms.Alert('验证码错误', '0');");
                    Response.End();
                }
                string _Content = GetCutString(Jumbot.Common.Strings.HtmlEncode(f("txtContent")), 200);
                if (Str2Int(f("replyid")) > 0)
                {
                    string sqlStr = "";
                    string joinStr = "a.UserId=B.Id";
                    string whereStr1 = "A.Id=" + Str2Str(f("replyid")) + " and A.Threadid=" + Str2Str(f("threadid")) + " and A.mtagid=" + Str2Str(f("mtagid"));
                    string whereStr2 = "Id=" + Str2Str(f("replyid")) + " and Threadid=" + Str2Str(f("threadid")) + " and mtagid=" + Str2Str(f("mtagid"));
                    sqlStr = Jumbot.Common.staticFunction.GetSql("a.id as replyid,a.Content as replyContent,b.id as replyuserid,b.UserName as replyusername", "jcms_normal_mtag_thread_reply", "jcms_normal_user", "Id", 1, 1, "desc", joinStr, whereStr1, whereStr2);
                    doh.Reset();
                    doh.SqlCmd = sqlStr;
                    DataTable dt = doh.GetDataTable();
                    _Content = "<div class=\"quote\"><span class=\"q\"><b>" + dt.Rows[0]["replyusername"].ToString() + "</b>: " + GetCutString(dt.Rows[0]["replyContent"].ToString(), 100) + "</span></div>" + _Content;
                    dt.Clear();
                    dt.Dispose();

                }
                doh.Reset();
                doh.AddFieldItem("Content", _Content);
                doh.AddFieldItem("MTagId", Str2Str(f("mtagid")));
                doh.AddFieldItem("ThreadId", Str2Str(f("threadid")));
                doh.AddFieldItem("AddTime", DateTime.Now.ToString());
                doh.AddFieldItem("UserId", UserId);
                int _newid = doh.Insert("jcms_normal_mtag_thread_reply");
                if (_newid > 0)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + Str2Str(f("threadid"));
                    doh.Add("jcms_normal_mtag_thread", "ReplyCount");
                    doh.Reset();
                    doh.ConditionExpress = "id=" + Str2Str(f("mtagid"));
                    doh.AddFieldItem("UpdateTime", DateTime.Now.ToString());
                    doh.Update("jcms_normal_mtag");
                    Response.Write("JumbotCms.Alert('回复成功', '1', \"window.location='" + site.Dir + "mtag/view_threadview/index.aspx?threadid=" + threadid + "&mtagid=" + mtagid + "';\");");
                }
                else
					Response.Write("JumbotCms.Alert('提交有误', '0');");
            }
        }
    }
}
